
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>十、数组链表操作 · 计算机笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="海拉尔的三角">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro-fixed/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-ancre-navigation/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-auto-scroll-table/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex-pro/katex.min.css">
                
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-terminull-light/terminull.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="find_withNum.html" />
    
    
    <link rel="prev" href="dataStruction_withNum.html" />
    

    
        <link rel="shortcut icon" href="image/ico.png" type="image/x-icon">
    
    
    
    
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="image/ico.png">
    
    
        
    
    
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://github.com/spite-triangle" target="_blank" class="custom-link">Github</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../cpp.html">
            
                <a href="../cpp.html">
            
                    
                    C/C++
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="foundation_withNum.html">
            
                <a href="foundation_withNum.html">
            
                    
                    一、基础概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="keyword_withNum.html">
            
                <a href="keyword_withNum.html">
            
                    
                    二、关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="point_reference_withNum.html">
            
                <a href="point_reference_withNum.html">
            
                    
                    三、指针与引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="oop_withNum.html">
            
                <a href="oop_withNum.html">
            
                    
                    四、面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="memory_withNum.html">
            
                <a href="memory_withNum.html">
            
                    
                    五、内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="process_withNum.html">
            
                <a href="process_withNum.html">
            
                    
                    六、进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="locks_withNum.html">
            
                <a href="locks_withNum.html">
            
                    
                    七、锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="stl_withNum.html">
            
                <a href="stl_withNum.html">
            
                    
                    八、STL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="dataStruction_withNum.html">
            
                <a href="dataStruction_withNum.html">
            
                    
                    九、数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.10" data-path="array_list_withNum.html">
            
                <a href="array_list_withNum.html">
            
                    
                    十、数组链表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.11" data-path="find_withNum.html">
            
                <a href="find_withNum.html">
            
                    
                    十一、目标查找
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.12" data-path="tree_withNum.html">
            
                <a href="tree_withNum.html">
            
                    
                    十二、树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.13" data-path="binaryTree_withNum.html">
            
                <a href="binaryTree_withNum.html">
            
                    
                    十三、二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.14" data-path="dynamicProgram_withNum.html">
            
                <a href="dynamicProgram_withNum.html">
            
                    
                    十四、动态规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.15" data-path="backtracking_withNum.html">
            
                <a href="backtracking_withNum.html">
            
                    
                    十五、回溯算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.16" data-path="monotoneStack_withNum.html">
            
                <a href="monotoneStack_withNum.html">
            
                    
                    十六、单调栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.17" data-path="toollib_withNum.html">
            
                <a href="toollib_withNum.html">
            
                    
                    附录 工具库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../MySQL/mysql.html">
            
                <a href="../../MySQL/mysql.html">
            
                    
                    MySQL
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../MySQL/chapter/introduction_withNum.html">
            
                <a href="../../MySQL/chapter/introduction_withNum.html">
            
                    
                    一、简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../MySQL/chapter/foundation_withNum.html">
            
                <a href="../../MySQL/chapter/foundation_withNum.html">
            
                    
                    二、控制台操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../MySQL/chapter/operatorDB_withNum.html">
            
                <a href="../../MySQL/chapter/operatorDB_withNum.html">
            
                    
                    三、数据库、表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../MySQL/chapter/operatorData_withNum.html">
            
                <a href="../../MySQL/chapter/operatorData_withNum.html">
            
                    
                    四、数据操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../MySQL/chapter/transaction_withNum.html">
            
                <a href="../../MySQL/chapter/transaction_withNum.html">
            
                    
                    五、事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../../MySQL/chapter/index_withNum.html">
            
                <a href="../../MySQL/chapter/index_withNum.html">
            
                    
                    六、索引
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../../MySQL/chapter/authority_withNum.html">
            
                <a href="../../MySQL/chapter/authority_withNum.html">
            
                    
                    七、权限管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../../MySQL/chapter/backup_withNum.html">
            
                <a href="../../MySQL/chapter/backup_withNum.html">
            
                    
                    八、备份
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../linux/linux.html">
            
                <a href="../../linux/linux.html">
            
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../linux/chapter/foundation_withNum.html">
            
                <a href="../../linux/chapter/foundation_withNum.html">
            
                    
                    一、原理知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../linux/chapter/command_withNum.html">
            
                <a href="../../linux/chapter/command_withNum.html">
            
                    
                    二、指令
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../tcp_udp/tcp_upd.html">
            
                <a href="../../tcp_udp/tcp_upd.html">
            
                    
                    网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../../tcp_udp/chapter/foundation_withNum.html">
            
                <a href="../../tcp_udp/chapter/foundation_withNum.html">
            
                    
                    一、网络通讯基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                <a href="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                    
                    二、tcp/udp
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >十、数组链表操作</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;"><b></b>&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#1-&#x6392;&#x5E8F;"><b></b>1. &#x6392;&#x5E8F;</a></li><ul><li><span class="title-icon "></span><a href="#11-&#x5192;&#x6CE1;&#x6392;&#x5E8F;"><b></b>1.1. &#x5192;&#x6CE1;&#x6392;&#x5E8F;</a></li><li><span class="title-icon "></span><a href="#12-&#x6876;&#x6392;&#x5E8F;"><b></b>1.2. &#x6876;&#x6392;&#x5E8F;</a></li><li><span class="title-icon "></span><a href="#13-&#x5FEB;&#x901F;&#x6392;&#x5E8F;"><b></b>1.3. &#x5FEB;&#x901F;&#x6392;&#x5E8F;</a></li></ul><li><span class="title-icon "></span><a href="#2-&#x94FE;&#x8868;"><b></b>2. &#x94FE;&#x8868;</a></li><ul><li><span class="title-icon "></span><a href="#21-&#x6784;&#x5EFA;&#x94FE;&#x8868;"><b></b>2.1. &#x6784;&#x5EFA;&#x94FE;&#x8868;</a></li><li><span class="title-icon "></span><a href="#22-&#x9006;&#x5411;"><b></b>2.2. &#x9006;&#x5411;</a></li><ul><li><span class="title-icon "></span><a href="#221-&#x6574;&#x4F53;&#x9006;&#x5411;-&#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;"><b></b>2.2.1. &#x6574;&#x4F53;&#x9006;&#x5411;: &#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;</a></li><li><span class="title-icon "></span><a href="#222-&#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;"><b></b>2.2.2. &#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;</a></li><li><span class="title-icon "></span><a href="#223-&#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;"><b></b>2.2.3. &#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;:</a></li></ul><li><span class="title-icon "></span><a href="#23-&#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;"><b></b>2.3. &#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;</a></li></ul><li><span class="title-icon "></span><a href="#3-&#x56DE;&#x6587;"><b></b>3. &#x56DE;&#x6587;</a></li><ul><li><span class="title-icon "></span><a href="#31-&#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;"><b></b>3.1. &#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;</a></li><li><span class="title-icon "></span><a href="#32-&#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;"><b></b>3.2. &#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;</a></li></ul><li><span class="title-icon "></span><a href="#4-&#x53CC;&#x6307;&#x9488;"><b></b>4. &#x53CC;&#x6307;&#x9488;</a></li><ul><li><span class="title-icon "></span><a href="#41-&#x5FEB;&#x6162;&#x6307;&#x9488;"><b></b>4.1. &#x5FEB;&#x6162;&#x6307;&#x9488;</a></li><ul><li><span class="title-icon "></span><a href="#411-&#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;"><b></b>4.1.1. &#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;</a></li><li><span class="title-icon "></span><a href="#412-&#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;"><b></b>4.1.2. &#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;</a></li><li><span class="title-icon "></span><a href="#413-&#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;"><b></b>4.1.3. &#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;</a></li><li><span class="title-icon "></span><a href="#414-&#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;"><b></b>4.1.4. &#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;</a></li><li><span class="title-icon "></span><a href="#415-&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x53BB;&#x91CD;"><b></b>4.1.5. &#x6709;&#x5E8F;&#x6570;&#x7EC4;/&#x94FE;&#x8868;&#x53BB;&#x91CD;</a></li><li><span class="title-icon "></span><a href="#416-&#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;"><b></b>4.1.6. &#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;</a></li><li><span class="title-icon "></span><a href="#417-&#x79FB;&#x52A8;&#x96F6;"><b></b>4.1.7. &#x79FB;&#x52A8;&#x96F6;</a></li></ul><li><span class="title-icon "></span><a href="#42-&#x5DE6;&#x53F3;&#x6307;&#x9488;"><b></b>4.2. &#x5DE6;&#x53F3;&#x6307;&#x9488;</a></li><ul><li><span class="title-icon "></span><a href="#421-&#x7530;&#x5FCC;&#x8D5B;&#x9A6C;"><b></b>4.2.1. &#x7530;&#x5FCC;&#x8D5B;&#x9A6C;</a></li></ul></ul></ul></div><a href="#&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 style="font-size:60px;text-align:center;" id="&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;"><a name="&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x64CD;&#x4F5C;</h1>

<h1 id="1-&#x6392;&#x5E8F;"><a name="1-&#x6392;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#1-&#x6392;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x6392;&#x5E8F;</h1>
<p style="text-align:center;"><a href="../../image/cpp/sortAlgorithm.png" data-lightbox="37c08fdb-3c2b-4ef0-ae7c-3a25c79e3ed0" data-title="undefined"><img src="../../image/cpp/sortAlgorithm.png" alt="undefined"></a></p>

<blockquote>
<p>[!tip|style:flat]
<strong>&#x7A33;&#x5B9A;&#x7684;&#x7B97;&#x6CD5;&#xFF1A;&#x63D2;&#x5192;&#x9F9F;&#xFF0C;JJ&#x75DB;&#x3002;(&#x63D2;&#x5165;&#xFF0C;&#x5192;&#x6CE1;&#xFF0C;&#x5F52;&#x5E76;&#xFF0C;&#x8BA1;&#x6570;&#xFF0C;&#x57FA;&#x6570;&#xFF0C;&#x6876;)</strong></p>
</blockquote>
<h2 id="11-&#x5192;&#x6CE1;&#x6392;&#x5E8F;"><a name="11-&#x5192;&#x6CE1;&#x6392;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#11-&#x5192;&#x6CE1;&#x6392;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x5192;&#x6CE1;&#x6392;&#x5E8F;</h2>
<ul>
<li><span style="color:red;font-weight:bold"> &#x7A33;&#x5B9A;&#x6392;&#x5E8F;&#xFF0C;&#x6392;&#x5E8F;&#x4E4B;&#x540E;&#xFF0C;&#x6570;&#x636E;&#x8F93;&#x5165;&#x987A;&#x5E8F;&#x4E0D;&#x4F1A;&#x4E71;&#x6389; </span></li>
</ul>
<pre class="language-"><code class="lang-cpp">string str<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> str<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> str<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&lt;</span> str<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">char</span> temp <span class="token operator">=</span> str<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
                str<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
                str<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span> 
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="12-&#x6876;&#x6392;&#x5E8F;"><a name="12-&#x6876;&#x6392;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#12-&#x6876;&#x6392;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x6876;&#x6392;&#x5E8F;</h2>
<ul>
<li><span style="color:red;font-weight:bold"> &#x6876;&#x6392;&#x5E8F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7A33;&#x5B9A;&#x6392;&#x5E8F; </span></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">int</span><span class="token operator">*</span> in<span class="token punctuation">;</span>
    <span class="token comment">// &#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x6876;</span>
    vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> <span class="token function">bucket</span><span class="token punctuation">(</span>amount<span class="token punctuation">,</span><span class="token generic-function"><span class="token function">vector</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x6392;&#x5E8F;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">size</span><span class="token punctuation">(</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        bucket<span class="token punctuation">[</span> <span class="token function">sortRule</span><span class="token punctuation">(</span>in<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>in<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

    <span class="token comment">// &#x8F93;&#x51FA;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> bucket<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>bucket<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> bucket<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            bucket<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

    <span class="token punctuation">}</span>
</code></pre>
<h2 id="13-&#x5FEB;&#x901F;&#x6392;&#x5E8F;"><a name="13-&#x5FEB;&#x901F;&#x6392;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#13-&#x5FEB;&#x901F;&#x6392;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x5FEB;&#x901F;&#x6392;&#x5E8F;</h2>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">quickSort</span><span class="token punctuation">(</span><span class="token keyword">int</span> left<span class="token punctuation">,</span> <span class="token keyword">int</span> right<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">*</span> array<span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>left <span class="token operator">&gt;=</span> right<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> l <span class="token operator">=</span> left<span class="token punctuation">;</span>
    <span class="token keyword">int</span> r <span class="token operator">=</span> right<span class="token punctuation">;</span>
    <span class="token keyword">int</span> base <span class="token operator">=</span> array<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span> <span class="token punctuation">(</span>l <span class="token operator">&lt;</span> r<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment">// &#x79FB;&#x52A8;&#x53F3;&#x8FB9;&#x6307;&#x9488;&#xFF0C;&#x627E;&#x5C0F;&#x4E8E; base &#x7684;&#x503C;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span> r<span class="token punctuation">;</span> r <span class="token operator">&gt;</span> l <span class="token punctuation">;</span> r <span class="token operator">--</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>array<span class="token punctuation">[</span>r<span class="token punctuation">]</span> <span class="token operator">&lt;</span> base<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> 
        <span class="token comment">// &#x79FB;&#x52A8;&#x5DE6;&#x8FB9;&#x6307;&#x9488;&#xFF0C;&#x627E;&#x5927;&#x4E8E; base &#x7684;&#x503C;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span>l<span class="token punctuation">;</span> l <span class="token operator">&lt;</span> r<span class="token punctuation">;</span> l<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>array<span class="token punctuation">[</span>l<span class="token punctuation">]</span> <span class="token operator">&gt;</span> base<span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// &#x4EA4;&#x6362;&#x5DE6;&#x53F3;&#x503C;&#xFF1B;&#x4E24;&#x6307;&#x9488;&#x76F8;&#x9047;&#xFF0C;&#x5C31;&#x628A; base &#x653E;&#x5230;&#x4E2D;&#x95F4;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>l <span class="token operator">==</span> r<span class="token punctuation">)</span><span class="token punctuation">{</span>
            array<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">=</span> array<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span>
            array<span class="token punctuation">[</span>l<span class="token punctuation">]</span> <span class="token operator">=</span> base<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token keyword">int</span> temp <span class="token operator">=</span> array<span class="token punctuation">[</span>l<span class="token punctuation">]</span><span class="token punctuation">;</span>
            array<span class="token punctuation">[</span>l<span class="token punctuation">]</span> <span class="token operator">=</span> array<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">;</span>
            array<span class="token punctuation">[</span>r<span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">quickSort</span><span class="token punctuation">(</span>left<span class="token punctuation">,</span>l<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">quickSort</span><span class="token punctuation">(</span>l<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>right<span class="token punctuation">,</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="2-&#x94FE;&#x8868;"><a name="2-&#x94FE;&#x8868;" class="anchor-navigation-ex-anchor" href="#2-&#x94FE;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>2. &#x94FE;&#x8868;</h1>
<h2 id="21-&#x6784;&#x5EFA;&#x94FE;&#x8868;"><a name="21-&#x6784;&#x5EFA;&#x94FE;&#x8868;" class="anchor-navigation-ex-anchor" href="#21-&#x6784;&#x5EFA;&#x94FE;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. &#x6784;&#x5EFA;&#x94FE;&#x8868;</h2>
<ul>
<li><span style="color:red;font-weight:bold"> &#x5728;&#x5934;&#x90E8;&#x53E0;&#x52A0; </span></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">appendNode</span><span class="token punctuation">(</span><span class="token keyword">int</span> val<span class="token punctuation">)</span><span class="token punctuation">{</span>
        Node<span class="token operator">*</span> newNode <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">Node</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>last <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token comment">// &#x5728;&#x5934;&#x90E8;&#x53E0;&#x52A0;</span>
            newNode<span class="token operator">-&gt;</span>next <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-&gt;</span>head<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        size<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<ul>
<li><span style="color:red;font-weight:bold"> &#x5728;&#x5C3E;&#x90E8;&#x589E;&#x52A0; </span></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">pushNode</span><span class="token punctuation">(</span><span class="token keyword">int</span> val<span class="token punctuation">)</span><span class="token punctuation">{</span>
        Node <span class="token operator">*</span>newNode <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">Node</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>last <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token comment">// &#x5728;&#x5C3E;&#x90E8;&#x6DFB;&#x52A0;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>last<span class="token operator">-&gt;</span>next <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token operator">-&gt;</span>last <span class="token operator">=</span> newNode<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        size <span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="22-&#x9006;&#x5411;"><a name="22-&#x9006;&#x5411;" class="anchor-navigation-ex-anchor" href="#22-&#x9006;&#x5411;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. &#x9006;&#x5411;</h2>
<h3 id="221-&#x6574;&#x4F53;&#x9006;&#x5411;-&#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;"><a name="221-&#x6574;&#x4F53;&#x9006;&#x5411;-&#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;" class="anchor-navigation-ex-anchor" href="#221-&#x6574;&#x4F53;&#x9006;&#x5411;-&#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2.1. &#x6574;&#x4F53;&#x9006;&#x5411;: &#x9760;&#x9012;&#x5F52;&#x7684;&#x8FD4;&#x56DE;&#x8FC7;&#x7A0B;&#xFF0C;&#x5B8C;&#x6210;&#x53CD;&#x8F6C;</h3>
<pre class="language-"><code class="lang-cpp">ListNode <span class="token function">reverse</span><span class="token punctuation">(</span>ListNode head<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// &#x6DF1;&#x5165;&#x7684;&#x7EC8;&#x6B62;&#x8C03;&#x8282;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>head<span class="token punctuation">.</span>next <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span>

    <span class="token comment">// &#x8FD4;&#x56DE;&#x7684;&#x94FE;&#x8868;&#x7684;&#x5C3E;&#x8282;&#x70B9;&#x662F; head.next</span>
    ListNode last <span class="token operator">=</span> <span class="token function">reverse</span><span class="token punctuation">(</span>head<span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5728;&#x5C3E;&#x8282;&#x70B9;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
    head<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next <span class="token operator">=</span> head<span class="token punctuation">;</span>

    <span class="token comment">// &#x65AD;&#x5F00;&#x4E4B;&#x524D;&#x65B9;&#x5411;&#x7684;&#x94FE;&#x63A5;</span>
    head<span class="token punctuation">.</span>next <span class="token operator">=</span> null<span class="token punctuation">;</span>

    <span class="token keyword">return</span> last<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="222-&#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;"><a name="222-&#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;" class="anchor-navigation-ex-anchor" href="#222-&#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2.2. &#x4ECE;&#x9996;&#x5F00;&#x59CB;&#x7684;n&#x4E2A;&#x5C40;&#x90E8;&#x9006;&#x5411;</h3>
<p><strong>&#x5C06;<code>n+1</code>&#x8282;&#x70B9;&#x4F4D;&#x7F6E;&#x50A8;&#x5B58;&#xFF0C;&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x9012;&#x5F52;&#x8FDB;&#x884C;&#x9006;&#x5411;&#xFF0C;&#x6700;&#x540E;&#x628A; <code>n+1</code> &#x8282;&#x70B9;&#x63A5;&#x56DE;&#x53BB;&#x3002;</strong></p>
<pre class="language-"><code class="lang-cpp">ListNode successor <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token comment">// &#x8BB0;&#x5F55;&#x7B2C; n + 1 &#x4E2A;&#x8282;&#x70B9;</span>

<span class="token comment">// &#x53CD;&#x8F6C;&#x4EE5; head &#x4E3A;&#x8D77;&#x70B9;&#x7684; n &#x4E2A;&#x8282;&#x70B9;&#xFF0C;&#x8FD4;&#x56DE;&#x65B0;&#x7684;&#x5934;&#x7ED3;&#x70B9;</span>
ListNode <span class="token function">reverseN</span><span class="token punctuation">(</span>ListNode head<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
        <span class="token comment">// &#x8BB0;&#x5F55;&#x7B2C; n + 1 &#x4E2A;&#x8282;&#x70B9;</span>
        successor <span class="token operator">=</span> head<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        <span class="token keyword">return</span> head<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x4EE5; head.next &#x4E3A;&#x8D77;&#x70B9;&#xFF0C;&#x9700;&#x8981;&#x53CD;&#x8F6C;&#x524D; n - 1 &#x4E2A;&#x8282;&#x70B9;</span>
    ListNode last <span class="token operator">=</span> <span class="token function">reverseN</span><span class="token punctuation">(</span>head<span class="token punctuation">.</span>next<span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    head<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token comment">// &#x8BA9;&#x53CD;&#x8F6C;&#x4E4B;&#x540E;&#x7684;&#x5C3E;&#x8282;&#x70B9; head &#x548C;&#x540E;&#x9762;&#x7684;&#x8282;&#x70B9;&#x8FDE;&#x8D77;&#x6765;</span>
    head<span class="token punctuation">.</span>next <span class="token operator">=</span> successor<span class="token punctuation">;</span>
    <span class="token keyword">return</span> last<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="223-&#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;"><a name="223-&#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;" class="anchor-navigation-ex-anchor" href="#223-&#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2.3. &#x5BF9;m&#x5230;n&#x7684;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x9006;&#x5411;:</h3>
<p><strong>&#x627E;&#x5230;&#x5BF9;<code>m-1</code>&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x8BB0;&#x5F55;&#xFF0C;&#x7136;&#x540E;&#x7528;&#x4E0A;&#x9762;&#x65B9;&#x6CD5;&#x9006;&#x5411;&#xFF0C;&#x7136;&#x540E;&#x63A5;&#x56DE;&#x53BB;&#x3002;</strong></p>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x627E;&#x5230;&#x7B2C;m&#x4E2A;&#x8282;&#x70B9;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x8282;&#x70B9;</span>
<span class="token class-name">ListNode</span> <span class="token function">reverseBetween</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">,</span> <span class="token keyword">int</span> m<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// base case</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>m <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">reverseN</span><span class="token punctuation">(</span>head<span class="token punctuation">,</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x524D;&#x8FDB;&#x5230;&#x53CD;&#x8F6C;&#x7684;&#x8D77;&#x70B9;&#x89E6;&#x53D1; base case&#xFF0C;n&#x4E5F;&#x7528;&#x51CF; 1 &#xFF0C;&#x4E00;&#x4F1A;&#x513F;&#x53CD;&#x8F6C;&#x662F;&#x6309;&#x7167;&#x603B;&#x957F;&#x5EA6;&#x6765;&#x7684;</span>
    head<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token function">reverseBetween</span><span class="token punctuation">(</span>head<span class="token punctuation">.</span>next<span class="token punctuation">,</span> m <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> head<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="23-&#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;"><a name="23-&#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;" class="anchor-navigation-ex-anchor" href="#23-&#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3. &#x4E00;&#x5757;&#x4E00;&#x5757;&#x9006;&#x5411;</h2>
<ul>
<li><strong>&#x5FAA;&#x73AF;&#x5B9E;&#x73B0;:</strong> <strong>&#x5C06;&#x5F53;&#x524D;&#x8282;&#x70B9;&#xFF0C;&#x7528;&#x5934;&#x65B9;&#x5411;&#x589E;&#x957F;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x91CD;&#x65B0;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x94FE;&#x8868;</strong></li>
</ul>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">reverse</span><span class="token punctuation">(</span>Node<span class="token operator">*</span> head<span class="token punctuation">)</span><span class="token punctuation">{</span>
        Node<span class="token operator">*</span> inverseHead <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
        Node<span class="token operator">*</span> currentNode <span class="token operator">=</span> head<span class="token punctuation">;</span>
        Node<span class="token operator">*</span> temp <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>

        <span class="token keyword">this</span><span class="token operator">-&gt;</span>last <span class="token operator">=</span> currentNode<span class="token punctuation">;</span>

        <span class="token keyword">while</span> <span class="token punctuation">(</span> currentNode <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">// &#x7531;&#x4E8E;&#x8FD8;&#x8981;&#x7528;&#xFF0C;&#x4E34;&#x65F6;&#x5B58;&#x4E00;&#x4E0B;</span>
            temp <span class="token operator">=</span> currentNode<span class="token operator">-&gt;</span>next<span class="token punctuation">;</span>

            <span class="token comment">// &#x5C06;&#x5F53;&#x524D;&#x7684;&#x8282;&#x70B9;&#x7EC4;&#x6210;&#x65B0;&#x7684;&#x94FE;&#x8868;&#xFF0C;&#x91C7;&#x7528;&#x5934;&#x90E8;&#x589E;&#x957F;&#x7684;&#x65B9;&#x5F0F; </span>
            currentNode<span class="token operator">-&gt;</span>next <span class="token operator">=</span> inverseHead<span class="token punctuation">;</span>
            inverseHead <span class="token operator">=</span> currentNode<span class="token punctuation">;</span>

            <span class="token comment">// &#x66F4;&#x65B0;</span>
            currentNode <span class="token operator">=</span> temp<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">this</span><span class="token operator">-&gt;</span>head <span class="token operator">=</span> inverseHead<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<ul>
<li><strong>&#x5757;&#x9006;&#x5411;:</strong> <strong>&#x9012;&#x5F52;&#x6DF1;&#x5165;&#x62C6;&#x5206;&#x5757;&#xFF0C;&#x56DE;&#x9000;&#x62FC;&#x63A5;&#x5757;</strong><ul>
<li><code>reverse(a, b)</code>: <strong>&#x7FFB;&#x8F6C;&#x7684;&#x533A;&#x95F4;&#x4E3A;&#xFF1A;[a,b)</strong></li>
</ul>
</li>
</ul>
<pre class="language-"><code class="lang-cpp">ListNode <span class="token function">reverseKGroup</span><span class="token punctuation">(</span>ListNode head<span class="token punctuation">,</span> <span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>head <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span>
    <span class="token comment">// &#x533A;&#x95F4; [a, b) &#x5305;&#x542B; k &#x4E2A;&#x5F85;&#x53CD;&#x8F6C;&#x5143;&#x7D20;</span>
    ListNode a<span class="token punctuation">,</span> b<span class="token punctuation">;</span>
    a <span class="token operator">=</span> b <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token comment">// &#x5FAA;&#x73AF;&#x5B8C;&#x6BD5;&#x540E;&#xFF0C;b&#x5DF2;&#x7ECF;&#x5230;&#x4E86; a+k &#xFF0C;[a&#xFF0C;b]&#x4E2D;&#x95F4;&#x95F4;&#x9694;&#x4E86; k+1 &#x4E2A;&#x8282;&#x70B9;&#x4E86;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> k<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x4E0D;&#x8DB3; k &#x4E2A;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x53CD;&#x8F6C;&#xFF0C;base case</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>b <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span> head<span class="token punctuation">;</span>
        b <span class="token operator">=</span> b<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x8FDB;&#x884C;[a&#xFF0C;b)&#x7684;&#x7FFB;&#x8F6C;&#xFF0C;a&#x5C31;&#x662F;&#x5C3E;&#x5DF4;&#xFF0C;&#x5165;&#x6808;</span>
    ListNode newHead <span class="token operator">=</span> <span class="token function">reverse</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x51FA;&#x6808;&#xFF0C;&#x5C06;&#x524D;&#x9762;&#x62C6;&#x5206;&#x7684;&#x5757;&#x53C8;&#x63A5;&#x56DE;&#x53BB;</span>
    a<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token function">reverseKGroup</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> k<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> newHead<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="3-&#x56DE;&#x6587;"><a name="3-&#x56DE;&#x6587;" class="anchor-navigation-ex-anchor" href="#3-&#x56DE;&#x6587;"><i class="fa fa-link" aria-hidden="true"></i></a>3. &#x56DE;&#x6587;</h1>
<h2 id="31-&#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;"><a name="31-&#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;" class="anchor-navigation-ex-anchor" href="#31-&#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1. &#x5B57;&#x7B26;&#x904D;&#x5386;&#x627E;&#x56DE;&#x6587;</h2>
<pre class="language-"><code class="lang-cpp">string <span class="token function">palindrome</span><span class="token punctuation">(</span>string<span class="token operator">&amp;</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> l<span class="token punctuation">,</span> <span class="token keyword">int</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x9632;&#x6B62;&#x7D22;&#x5F15;&#x8D8A;&#x754C;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>l <span class="token operator">&gt;=</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> r <span class="token operator">&lt;</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token operator">&amp;&amp;</span> s<span class="token punctuation">[</span>l<span class="token punctuation">]</span> <span class="token operator">==</span> s<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5411;&#x4E24;&#x8FB9;&#x5C55;&#x5F00;</span>
        l<span class="token operator">--</span><span class="token punctuation">;</span> r<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// l&#x4E0E;r&#x5728;&#x9000;&#x51FA;&#x5FAA;&#x73AF;&#x65F6;&#xFF0C;&#x53C8;&#x591A;&#x7B97;&#x4E86;&#x4E00;&#x6B21;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x8FD8;&#x539F;</span>
    <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span>l <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> r <span class="token operator">-</span> l <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="32-&#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;"><a name="32-&#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;" class="anchor-navigation-ex-anchor" href="#32-&#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;"><i class="fa fa-link" aria-hidden="true"></i></a>3.2. &#x5224;&#x65AD;&#x94FE;&#x8868;&#x662F;&#x5426;&#x662F;&#x56DE;&#x6587;</h2>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x529E;&#x6CD5;&#x5C31;&#x662F;&#xFF0C;&#x628A;&#x539F;&#x59CB;&#x94FE;&#x8868;&#x53CD;&#x8F6C;&#x5B58;&#x5165;&#x4E00;&#x6761;&#x65B0;&#x7684;&#x94FE;&#x8868;&#xFF0C;&#x7136;&#x540E;&#x6BD4;&#x8F83;&#x8FD9;&#x4E24;&#x6761;&#x94FE;&#x8868;&#x662F;&#x5426;&#x76F8;&#x540C;&#x3002;</p>
<pre class="language-"><code class="lang-java"><span class="token comment">// &#x5DE6;&#x4FA7;&#x6307;&#x9488;</span>
<span class="token class-name">ListNode</span> left<span class="token punctuation">;</span>

<span class="token keyword">boolean</span> <span class="token function">isPalindrome</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    left <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">traverse</span><span class="token punctuation">(</span>head<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">boolean</span> <span class="token function">traverse</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>right <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token keyword">boolean</span> res <span class="token operator">=</span> <span class="token function">traverse</span><span class="token punctuation">(</span>right<span class="token punctuation">.</span>next<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x540E;&#x5E8F;&#x904D;&#x5386;&#x4EE3;&#x7801;</span>
    res <span class="token operator">=</span> res <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>right<span class="token punctuation">.</span>val <span class="token operator">==</span> left<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span>
    left <span class="token operator">=</span> left<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token keyword">return</span> res<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="4-&#x53CC;&#x6307;&#x9488;"><a name="4-&#x53CC;&#x6307;&#x9488;" class="anchor-navigation-ex-anchor" href="#4-&#x53CC;&#x6307;&#x9488;"><i class="fa fa-link" aria-hidden="true"></i></a>4. &#x53CC;&#x6307;&#x9488;</h1>
<h2 id="41-&#x5FEB;&#x6162;&#x6307;&#x9488;"><a name="41-&#x5FEB;&#x6162;&#x6307;&#x9488;" class="anchor-navigation-ex-anchor" href="#41-&#x5FEB;&#x6162;&#x6307;&#x9488;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1. &#x5FEB;&#x6162;&#x6307;&#x9488;</h2>
<p>&#x5FEB;&#x6162;&#x6307;&#x9488;&#x4E00;&#x822C;&#x90FD;&#x521D;&#x59CB;&#x5316;&#x6307;&#x5411;&#x94FE;&#x8868;&#x7684;&#x5934;&#x7ED3;&#x70B9;<code>head</code>&#xFF0C;&#x524D;&#x8FDB;&#x65F6;&#x5FEB;&#x6307;&#x9488;<code>fast</code>&#x5728;&#x524D;&#xFF0C;&#x6162;&#x6307;&#x9488;<code>slow</code>&#x5728;&#x540E;&#x3002;</p>
<h3 id="411-&#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;"><a name="411-&#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;" class="anchor-navigation-ex-anchor" href="#411-&#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.1. &#x5224;&#x5B9A;&#x94FE;&#x8868;&#x4E2D;&#x662F;&#x5426;&#x542B;&#x6709;&#x73AF;</h3>
<font color="#FF0010">`fast`&#x7684;&#x79FB;&#x52A8;&#x901F;&#x5EA6;&#x4E3A;`slow`&#x7684;&#x4E24;&#x500D;&#x3002;</font>

<pre class="language-"><code class="lang-java"><span class="token keyword">boolean</span> <span class="token function">hasCycle</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">ListNode</span> fast<span class="token punctuation">,</span> slow<span class="token punctuation">;</span>
    fast <span class="token operator">=</span> slow <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> fast<span class="token punctuation">.</span>next <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>fast <span class="token operator">==</span> slow<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="412-&#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;"><a name="412-&#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;" class="anchor-navigation-ex-anchor" href="#412-&#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.2. &#x5DF2;&#x77E5;&#x94FE;&#x8868;&#x4E2D;&#x542B;&#x6709;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x73AF;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;</h3>
<pre class="language-"><code class="lang-java"><span class="token class-name">ListNode</span> <span class="token function">detectCycle</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">ListNode</span> fast<span class="token punctuation">,</span> slow<span class="token punctuation">;</span>
    fast <span class="token operator">=</span> slow <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> fast<span class="token punctuation">.</span>next <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>fast <span class="token operator">==</span> slow<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7C7B;&#x4F3C; hasCycle &#x51FD;&#x6570;</span>
    slow <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>slow <span class="token operator">!=</span> fast<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> slow<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><a href="../../image/cpp/doublePoint.jpeg" data-lightbox="5be5c4d0-ed52-440b-af1e-727159d23ce7" data-title="double point"><img src="../../image/cpp/doublePoint.jpeg" alt="double point"></a></p>
<p><code>fast</code>&#x4E00;&#x5B9A;&#x6BD4;<code>slow</code>&#x591A;&#x8D70;&#x4E86;k&#x6B65;&#xFF0C;&#x8FD9;&#x591A;&#x8D70;&#x7684;k&#x6B65;&#x5176;&#x5B9E;&#x5C31;&#x662F;<code>fast</code>&#x6307;&#x9488;&#x5728;&#x73AF;&#x91CC;&#x8F6C;&#x5708;&#x5708;&#xFF0C;&#x6240;&#x4EE5;<strong>k&#x7684;&#x503C;&#x5C31;&#x662F;&#x73AF;&#x957F;&#x5EA6;&#x7684;&#x300C;&#x6574;&#x6570;&#x500D;&#x300D;</strong>&#x3002;</p>
<h3 id="413-&#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;"><a name="413-&#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;" class="anchor-navigation-ex-anchor" href="#413-&#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.3. &#x94FE;&#x8868;&#x7684;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;</h3>
<pre class="language-"><code class="lang-java"><span class="token class-name">ListNode</span> <span class="token function">middleNode</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">ListNode</span> fast<span class="token punctuation">,</span> slow<span class="token punctuation">;</span>
    fast <span class="token operator">=</span> slow <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> fast<span class="token punctuation">.</span>next <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// slow &#x5C31;&#x5728;&#x4E2D;&#x95F4;&#x4F4D;&#x7F6E;</span>
    <span class="token keyword">return</span> slow<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x94FE;&#x8868;&#x7684;&#x957F;&#x5EA6;&#x662F;<strong>&#x5947;&#x6570;</strong>&#xFF0C;<code>slow</code>&#x6070;&#x5DE7;&#x505C;&#x5728;<strong>&#x4E2D;&#x70B9;&#x4F4D;&#x7F6E;</strong>
&#x94FE;&#x8868;&#x7684;&#x957F;&#x5EA6;&#x662F;<strong>&#x5076;&#x6570;</strong>&#xFF0C;<code>slow</code>&#x6700;&#x7EC8;&#x7684;&#x4F4D;&#x7F6E;&#x662F;<strong>&#x4E2D;&#x95F4;&#x504F;&#x53F3;</strong>&#xFF1A;</p>
<h3 id="414-&#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;"><a name="414-&#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;" class="anchor-navigation-ex-anchor" href="#414-&#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.4. &#x5BFB;&#x627E;&#x94FE;&#x8868;&#x7684;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x5143;&#x7D20;</h3>
<p>&#x8BA9;&#x5FEB;&#x6307;&#x9488;&#x5148;&#x8D70;n&#x6B65;&#xFF0C;&#x7136;&#x540E;&#x5FEB;&#x6162;&#x6307;&#x9488;&#x5F00;&#x59CB;&#x540C;&#x901F;&#x524D;&#x8FDB;;&#x8FD9;&#x6837;&#x5F53;&#x5FEB;&#x6307;&#x9488;&#x8D70;&#x5230;&#x94FE;&#x8868;&#x672B;&#x5C3E;null&#x65F6;&#xFF0C;&#x6162;&#x6307;&#x9488;&#x6240;&#x5728;&#x7684;&#x4F4D;&#x7F6E;&#x5C31;&#x662F;&#x5012;&#x6570;&#x7B2C;n&#x4E2A;&#x94FE;&#x8868;&#x8282;&#x70B9;&#xFF08;n&#x4E0D;&#x4F1A;&#x8D85;&#x8FC7;&#x94FE;&#x8868;&#x957F;&#x5EA6;&#xFF09;</p>
<pre class="language-"><code class="lang-java"><span class="token class-name">ListNode</span> <span class="token function">removeNthFromEnd</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">ListNode</span> fast<span class="token punctuation">,</span> slow<span class="token punctuation">;</span>
    fast <span class="token operator">=</span> slow <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token comment">// &#x5FEB;&#x6307;&#x9488;&#x5148;&#x524D;&#x8FDB; n &#x6B65;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>n<span class="token operator">--</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fast <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5982;&#x679C;&#x6B64;&#x65F6;&#x5FEB;&#x6307;&#x9488;&#x8D70;&#x5230;&#x5934;&#x4E86;&#xFF0C;</span>
        <span class="token comment">// &#x8BF4;&#x660E;&#x5012;&#x6570;&#x7B2C; n &#x4E2A;&#x8282;&#x70B9;&#x5C31;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
        <span class="token keyword">return</span> head<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x8BA9;&#x6162;&#x6307;&#x9488;&#x548C;&#x5FEB;&#x6307;&#x9488;&#x540C;&#x6B65;&#x5411;&#x524D;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> fast<span class="token punctuation">.</span>next <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// slow.next &#x5C31;&#x662F;&#x5012;&#x6570;&#x7B2C; n &#x4E2A;&#x8282;&#x70B9;&#xFF0C;&#x5220;&#x9664;&#x5B83;</span>
    slow<span class="token punctuation">.</span>next <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token keyword">return</span> head<span class="token punctuation">;</span>
</code></pre>
<h3 id="415-&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x53BB;&#x91CD;"><a name="415-&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x53BB;&#x91CD;" class="anchor-navigation-ex-anchor" href="#415-&#x6709;&#x5E8F;&#x6570;&#x7EC4;&#x94FE;&#x8868;&#x53BB;&#x91CD;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.5. &#x6709;&#x5E8F;&#x6570;&#x7EC4;/&#x94FE;&#x8868;&#x53BB;&#x91CD;</h3>
<p>&#x6570;&#x7EC4;<code>nums[]</code>&#x6709;&#x987A;&#x5E8F;&#xFF0C;<code>slow</code>&#x8D70;&#x5728;&#x540E;&#x9762;&#xFF0C;&#x5FEB;&#x6307;&#x9488;<code>fast</code>&#x8D70;&#x5728;&#x524D;&#x9762;&#x63A2;&#x8DEF;&#xFF0C;&#x6BD4;&#x8F83;<code>nums[fast]</code>&#x4E0E;<code>nums[slow]</code>&#xFF0C;&#x627E;&#x5230;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x5143;&#x7D20;&#x5C31;&#x544A;&#x8BC9;<code>slow</code>&#x5E76;&#x8BA9;<code>slow</code>&#x524D;&#x8FDB;&#x4E00;&#x6B65;&#x3002;<strong><code>nums[0  slow]</code>&#x4FBF;&#x662F;&#x53BB;&#x91CD;&#x540E;&#x7684;&#x6570;&#x7EC4;&#x3002;</strong></p>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">removeDuplicates</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> slow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> fast <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>fast<span class="token punctuation">]</span> <span class="token operator">!=</span> nums<span class="token punctuation">[</span>slow<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            slow<span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x7EF4;&#x62A4; nums[0..slow] &#x65E0;&#x91CD;&#x590D;</span>
            nums<span class="token punctuation">[</span>slow<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>fast<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        fast<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x4E3A;&#x7D22;&#x5F15; + 1</span>
    <span class="token keyword">return</span> slow <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class="language-"><code class="lang-java"><span class="token class-name">ListNode</span> <span class="token function">deleteDuplicates</span><span class="token punctuation">(</span><span class="token class-name">ListNode</span> head<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>head <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token class-name">ListNode</span> slow <span class="token operator">=</span> head<span class="token punctuation">,</span> fast <span class="token operator">=</span> head<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>fast<span class="token punctuation">.</span>val <span class="token operator">!=</span> slow<span class="token punctuation">.</span>val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// nums[slow] = nums[fast];</span>
            slow<span class="token punctuation">.</span>next <span class="token operator">=</span> fast<span class="token punctuation">;</span>
            <span class="token comment">// slow++;</span>
            slow <span class="token operator">=</span> slow<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// fast++</span>
        fast <span class="token operator">=</span> fast<span class="token punctuation">.</span>next<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x65AD;&#x5F00;&#x4E0E;&#x540E;&#x9762;&#x91CD;&#x590D;&#x5143;&#x7D20;&#x7684;&#x8FDE;&#x63A5;</span>
    slow<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> head<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>&#x6CE8;: &#x7531;&#x4E8E;c++&#x8FD8;&#x9700;&#x8981;&#x5BF9;new&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x624B;&#x52A8;&#x91CA;&#x653E;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x7528;&#x6570;&#x7EC4;&#x6765;&#x50A8;&#x5B58;&#x94FE;&#x8868;&#x8282;&#x70B9;&#xFF0C;&#x6216;&#x8005;&#x4F7F;&#x7528;&#x667A;&#x80FD;&#x6307;&#x9488;&#x3002;</strong></p>
<h3 id="416-&#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;"><a name="416-&#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#416-&#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.6. &#x5220;&#x9664;&#x76EE;&#x6807;&#x5143;&#x7D20;&#xFF0C;&#x4E0D;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#x987A;&#x5E8F;</h3>
<p>&#x4E0D;&#x8981;&#x6C42;&#x6570;&#x7EC4;&#x6709;&#x5E8F;&#xFF0C;&#x5982;&#x679C;<code>fast</code>&#x9047;&#x5230;&#x9700;&#x8981;&#x53BB;&#x9664;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x8DF3;&#x8FC7;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x544A;&#x8BC9;<code>slow</code>&#x6307;&#x9488;&#xFF0C;&#x5E76;&#x8BA9;<code>slow</code>&#x524D;&#x8FDB;&#x4E00;&#x6B65;&#x3002;<strong><code>nums[0   slow-1]</code>&#x662F;&#x53BB;&#x9664;&#x5143;&#x7D20;&#x540E;&#x7684;&#x6570;&#x7EC4;</strong></p>
<pre class="language-"><code class="lang-java"><span class="token keyword">int</span> <span class="token function">removeElement</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> fast <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> slow <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>fast <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>fast<span class="token punctuation">]</span> <span class="token operator">!=</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            nums<span class="token punctuation">[</span>slow<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>fast<span class="token punctuation">]</span><span class="token punctuation">;</span>
            slow<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        fast<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> slow<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="417-&#x79FB;&#x52A8;&#x96F6;"><a name="417-&#x79FB;&#x52A8;&#x96F6;" class="anchor-navigation-ex-anchor" href="#417-&#x79FB;&#x52A8;&#x96F6;"><i class="fa fa-link" aria-hidden="true"></i></a>4.1.7. &#x79FB;&#x52A8;&#x96F6;</h3>
<p>&#x4E0A;&#x4E00;&#x95EE;&#x9898;&#x7684;&#x53D8;&#x79CD;</p>
<h2 id="42-&#x5DE6;&#x53F3;&#x6307;&#x9488;"><a name="42-&#x5DE6;&#x53F3;&#x6307;&#x9488;" class="anchor-navigation-ex-anchor" href="#42-&#x5DE6;&#x53F3;&#x6307;&#x9488;"><i class="fa fa-link" aria-hidden="true"></i></a>4.2. &#x5DE6;&#x53F3;&#x6307;&#x9488;</h2>
<p>&#x4E00;&#x822C;&#x521D;&#x59CB;&#x5316;&#x4E3A;: <code>left = 0, right = nums.length - 1</code></p>
<ul>
<li><strong>&#x4E8C;&#x5206;&#x67E5;&#x627E;</strong></li>
<li><strong>&#x7FFB;&#x8F6C;&#x6570;&#x7EC4;</strong></li>
<li><strong>&#x53CC;&#x6307;&#x9488;&#xFF0C;&#x5347;&#x5E8F;&#x5217;&#xFF0C;&#x6C42;&#x4E24;&#x6570;&#x4E4B;&#x548C;</strong></li>
</ul>
<h3 id="421-&#x7530;&#x5FCC;&#x8D5B;&#x9A6C;"><a name="421-&#x7530;&#x5FCC;&#x8D5B;&#x9A6C;" class="anchor-navigation-ex-anchor" href="#421-&#x7530;&#x5FCC;&#x8D5B;&#x9A6C;"><i class="fa fa-link" aria-hidden="true"></i></a>4.2.1. &#x7530;&#x5FCC;&#x8D5B;&#x9A6C;</h3>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">struct</span> <span class="token class-name">Entry</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> index<span class="token punctuation">;</span>
    <span class="token keyword">int</span> val<span class="token punctuation">;</span>

    <span class="token function">Entry</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">,</span> <span class="token keyword">int</span> val<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">index</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">val</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token function">Entry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">Compare</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">const</span> Entry <span class="token operator">&amp;</span> A<span class="token punctuation">,</span><span class="token keyword">const</span> Entry <span class="token operator">&amp;</span> B<span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>A<span class="token punctuation">.</span>val <span class="token operator">&gt;</span> B<span class="token punctuation">.</span>val<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> 
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">void</span> <span class="token function">optinalSequence</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span><span class="token operator">*</span> nums1<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">*</span> nums2<span class="token punctuation">,</span><span class="token keyword">int</span> n <span class="token punctuation">)</span><span class="token punctuation">{</span>
    vector<span class="token operator">&lt;</span>Entry<span class="token operator">&gt;</span> target<span class="token punctuation">;</span>
    vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> option<span class="token punctuation">;</span> 
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        target<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">Entry</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> nums1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        option<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>nums2<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x4ECE;&#x5927;&#x5230;&#x5C0F;</span>
    <span class="token function">sort</span><span class="token punctuation">(</span>target<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>target<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">Compare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x4ECE;&#x5C0F;&#x5230;&#x5927;</span>
    <span class="token function">sort</span><span class="token punctuation">(</span>option<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>option<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5DE6;&#x53F3;&#x6307;&#x9488;</span>
    <span class="token keyword">int</span> left <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
    <span class="token keyword">int</span> right <span class="token operator">=</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment">// &#x6253;&#x5F97;&#x8D62;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>option<span class="token punctuation">[</span>right<span class="token punctuation">]</span> <span class="token operator">&gt;</span> target<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            nums2<span class="token punctuation">[</span>target<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>index<span class="token punctuation">]</span> <span class="token operator">=</span> option<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">;</span>
            right<span class="token operator">--</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token comment">// &#x6253;&#x4E0D;&#x8D62;&#xFF0C;&#x5C31;&#x7528;&#x5C0F;&#x7684;&#x51D1;&#x6570;</span>
            nums2<span class="token punctuation">[</span>target<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>index<span class="token punctuation">]</span> <span class="token operator">=</span> option<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">;</span>
            left<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li><strong>&#x51CC;&#x4E71;&#x7684;&#x6570;&#x7EC4;&#x5148;&#x6392;&#x5E8F;</strong></li>
<li><strong><code>left</code>: &#x6307;&#x5411;&#x6700;&#x5C0F;&#x7684;&#x4E00;&#x7AEF;</strong></li>
<li><strong><code>right</code>: &#x6307;&#x5411;&#x6700;&#x5927;&#x7684;&#x4E00;&#x6BB5;</strong></li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="dataStruction_withNum.html" class="navigation navigation-prev " aria-label="Previous page: 九、数据结构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="find_withNum.html" class="navigation navigation-next " aria-label="Next page: 十一、目标查找">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"十、数组链表操作","level":"2.1.10","depth":2,"next":{"title":"十一、目标查找","level":"2.1.11","depth":2,"path":"cpp/chapter/find_withNum.md","ref":"cpp/chapter/find_withNum.md","articles":[]},"previous":{"title":"九、数据结构","level":"2.1.9","depth":2,"path":"cpp/chapter/dataStruction_withNum.md","ref":"cpp/chapter/dataStruction_withNum.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro-fixed","expandable-chapters-small","ancre-navigation","hide-element","code","splitter","insert-logo-link-website","-sharing","sharing-plus","prism","-highlight","flexible-alerts","lightbox","pageview-count","auto-scroll-table","katex-pro","favicon-absolute","sectionx","terminull-light","livereload"],"styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-tomorrow.css"],"lang":{"flow":"typescript"}},"livereload":{},"splitter":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"auto-scroll-table":{},"code":{"copyButtons":true},"terminull-light":{},"search-pro-fixed":{},"favicon-absolute":{"favicon":"image/ico.png","appleTouchIconPrecomposed152":"image/ico.png","appleTouchIconMore":{},"appleTouchIconPrecomposedMore":{}},"hide-element":{"elements":[".gitbook-link"]},"katex-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"lightbox":{"jquery":true,"sameUuid":false},"insert-logo-link-website":{"url":"image/logo.png","link":"https://space.bilibili.com/27206875","target":"_blank","style":"height: 230px"},"pageview-count":{},"ancre-navigation":{"showLevel":true,"associatedWithSummary":true,"mode":"float","printLog":false,"multipleH1":true,"float":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"expandable-chapters-small":{},"flexible-alerts":{"style":"callout","note":{"label":"Note","icon":"fa fa-info-circle","className":"info"},"tip":{"label":"Tip","icon":"fa fa-lightbulb-o","className":"tip"},"warning":{"label":"Warning","icon":"fa fa-exclamation-triangle","className":"warning"},"danger":{"label":"Attention","icon":"fa fa-ban","className":"danger"}},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":false,"facebook":false,"weibo":true,"whatsapp":false,"twitter":false,"pocket":false,"google":true,"qzone":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"海拉尔的三角","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机笔记","links":{"sidebar":{"Github":"https://github.com/spite-triangle"}},"gitbook":"*"},"file":{"path":"cpp/chapter/array_list_withNum.md","mtime":"2021-09-14T15:43:12.078Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-14T15:43:23.052Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-auto-scroll-table/plugin.js"></script>
        
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-terminull-light/terminull.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

